package com.example.app2

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.widget.Button
import com.example.sevicedemo.ILoginAidlInterface

class LoginActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.button).setOnClickListener {
            bindServiceAIDL()

        }
    }

    var mILoginAidlInterface: ILoginAidlInterface? = null
    private val mConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            mILoginAidlInterface = ILoginAidlInterface.Stub.asInterface(service)
            mILoginAidlInterface?.loginCallback(1, "验证通过，登录成功啦")
            finish();
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            println("MainActivity.onServiceDisconnected")
        }

    }

    private fun bindServiceAIDL() {
        println("MainActivity.bindServiceAIDL")
        //客户端绑定服务端
        val intent = Intent();
        val aidlPackageName = "com.example.sevicedemo"; // 远程 aidl 的包名
        val aidlRemoteServiceName = "com.example.sevicedemo.AIDLService"; // 要绑定的远程服务名
        intent.component = ComponentName(aidlPackageName, aidlRemoteServiceName);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }
}